<!DOCTYPE html>
<html>

<head>
	<title>登录</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/layui.css" media="all">
	<link rel="stylesheet" href="../../style/admin.css" media="all">
	<link rel="stylesheet" href="../../style/login.css" media="all">

</head>

<body onkeydown="KeyDown()">

	<!-- <div style="width: 345px;margin-left: 34%;margin-top: 10%;"> -->
	<div class="layadmin-user-login-main">
		<div class="layadmin-user-login-box layadmin-user-login-header">
			<h2>寻觅</h2>
			<p>后台管理系统</p>
		</div>
		<form class="layadmin-user-login-box layadmin-user-login-body layui-form" action="">
			<div class="layui-form-item">
				<label class="layadmin-user-login-icon layui-icon layui-icon-username" for="LAY-user-login-username"></label>
				<input type="text" name="suid" lay-verify="name" autocomplete="off" placeholder="用户名" class="layui-input"
					v-model="loginForm.name">
			</div>
			<div class="layui-form-item">
				<label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
				<input type="password" name="password" lay-verify="pwd" autocomplete="off" placeholder="密码" class="layui-input"
					v-model="loginForm.password">
			</div>
			<div class="layui-form-item">
				<div class="layui-row">
					<div class="layui-col-xs7">
						<label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>
						<input type="text" lay-verify="code" placeholder="图形验证码" class="layui-input" v-model="code">
					</div>
					<div class="layui-col-xs5">
						<div style="margin-left: 10px;">
							<canvas id="canvas" width="100" height="43" onclick="dj()" style="border: 1px solid #ccc;
						        border-radius: 5px;margin-left: 5px;"></canvas>
						</div>
					</div>
				</div>
			</div>

			<div class="layui-form-item">
				<button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="login">登录</button>
			</div>
		</form>
	</div>

	<script src="../../plugins/jquery/jquery.min.js"></script>
	<script src="../../plugins/vue/vue.min.js"></script>
	<script src="../../js/utils/md5.js"></script>
	<script src="../../js/utils/common.js"></script>
	<script src="../../js/user/login.js"></script>
	<script src="../../layui.js" charset="utf-8"></script>

	<script>
		layui.use(['form', 'layer'], function () {
			var form = layui.form,
				layer = layui.layer

			//自定义验证规则
			form.verify({
				name: function (value) {
					if (value.length == 0) {
						return '请输入用户名';
					}
				},
				pwd: function (value) {
					if (value.length == 0) {
						return '请输入密码';
					}
				},
				code: function (value) {
					var num = show_num.join("")
					if (value.length == 0) {
						return '请输入验证码';
					} else if (num != value) {
						return '验证码错误';
					}
				}
			});
			// 监听提交
			form.on('submit(login)', function (data) {
				var sUrl = GlobalSiteUrl + "/admin/login";
				var suid = data.field.suid;
				var password = $.md5(data.field.password)
				localStorage.setItem("password", password)
				$.post({
					url: sUrl,
					data: {
						suid: suid,
						password: password
					},
					success: function (res) {
						if (res.status == "200") {
							const data = res.data.list[0]
							layer.msg("欢迎您，" + data.role_name + " " + data.name, {
								time: 1500
							}, function () {
								localStorage.setItem("name", data.name)
								localStorage.setItem("avatar", data.avatar)
								localStorage.setItem("role_name", data.role_name)
								localStorage.setItem("suid", data.suid)
								localStorage.setItem("rid", data.rid)
								location.href = "../../index.html";
							});
						} else {
							layer.msg(res.msg, {
								time: 1500,
								anim: 6
							});
						}
					}
				});
				return false;
			});
		});
	</script>


	<script>
		function KeyDown() {
			if (window.event.keyCode == 13) {
				if (document.all('login_btn') != null) {
					document.all('login_btn').click();
				}
			}
		}
	</script>

	<script>
		var show_num = [];
		draw(show_num);

		function dj() {
			draw(show_num);
		}

		function draw(show_num) {
			var canvas_width = document.getElementById('canvas').clientWidth;
			var canvas_height = document.getElementById('canvas').clientHeight;
			var canvas = document.getElementById("canvas"); //获取到canvas的对象，演员
			var context = canvas.getContext("2d"); //获取到canvas画图的环境，演员表演的舞台
			canvas.width = canvas_width;
			canvas.height = canvas_height;
			var sCode =
				"1,2,3,4,5,6,7,8,9,0,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
			var aCode = sCode.split(",");
			var aLength = aCode.length; //获取到数组的长度

			for (var i = 0; i <= 3; i++) {
				var j = Math.floor(Math.random() * aLength); //获取到随机的索引值
				var deg = Math.random() * 30 * Math.PI / 180; //产生0~30之间的随机弧度
				var txt = aCode[j]; //得到随机的一个内容
				show_num[i] = txt;
				var x = 10 + i * 20; //文字在canvas上的x坐标
				var y = 20 + Math.random() * 8; //文字在canvas上的y坐标
				context.font = "bold 23px 微软雅黑";

				context.translate(x, y);
				context.rotate(deg);

				context.fillStyle = randomColor();
				context.fillText(txt, 0, 0);

				context.rotate(-deg);
				context.translate(-x, -y);
			}
			for (var i = 0; i <= 5; i++) { //验证码上显示线条
				context.strokeStyle = randomColor();
				context.beginPath();
				context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
				context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
				context.stroke();
			}
			for (var i = 0; i <= 30; i++) { //验证码上显示小点
				context.strokeStyle = randomColor();
				context.beginPath();
				var x = Math.random() * canvas_width;
				var y = Math.random() * canvas_height;
				context.moveTo(x, y);
				context.lineTo(x + 1, y + 1);
				context.stroke();
			}
		}

		function randomColor() { //得到随机的颜色值
			var r = Math.floor(Math.random() * 256);
			var g = Math.floor(Math.random() * 256);
			var b = Math.floor(Math.random() * 256);
			return "rgb(" + r + "," + g + "," + b + ")";
		}
	</script>

</body>

</html>